import user from '@/lib/request/user'
import { RootState } from '../index'
import { Commit } from 'vuex'
export interface UserState {
  accountLicenseNumber: string
  accountLicenseType: number
  accountMobile: string
  accountName: string
  accountUUID: string
  organizeAdministrationLevel: number
  organizeLicenseNumber: string
  organizeLicenseType: number
  organizeName: string
  organizeType: number
  organizeUUID: string
  accountEmail: string
  bindDingUserVisible: boolean
  dingUserId: string
  ignoreUploadApplyExampleHint: boolean
}
const initState: UserState = {
  accountLicenseNumber: '',
  accountLicenseType: 1,
  accountMobile: '',
  accountName: '',
  accountUUID: '',
  organizeAdministrationLevel: 1,
  organizeLicenseNumber: '',
  organizeLicenseType: 2,
  organizeName: '',
  organizeType: 3,
  organizeUUID: '',
  accountEmail: '',
  bindDingUserVisible: false,
  dingUserId: '',
  ignoreUploadApplyExampleHint: false,
}

const getters = {}

const actions = {
  async info(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return new Promise(async (resolve: any, reject: any) => {
      const data: any = await user.getInfo(payload).catch(e => {
        reject(e)
      })
      if (data) {
        context.commit('setStateData', (data || {}).data)
        resolve(data.data)
      } else {
        reject()
      }
    })
  },
  sendMobileCode(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload?: any
  ) {
    return user.simpleSendMobileCode()
  },
  saveInfo(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.modifyAccount(payload)
  },
  ignoreBindingHint(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.ignoreBindingHint()
  },
  queryDingAccountByMobile(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.queryDingAccountByMobile(payload)
  },
  bindingDingAccount(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.bindingDingAccount(payload)
  },
  accountDataMasking(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.accountDataMasking(payload)
  },
  payOrganizeDataMasking(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.payOrganizeDataMasking(payload)
  },
  modifyMobileVerify(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.modifyMobileVerify(payload)
  },
  sendRebindingMsg(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.sendRebindingMsg(payload)
  },
  rebindTheMobile(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.rebindTheMobile(payload)
  },
  ignoreUploadApplyExampleHint(
    context: {
      commit: Commit
      state: UserState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.ignoreUploadApplyExampleHint(payload)
  },
}

const mutations = {
  setStateData(state: UserState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
